/**
* @project admin-go-api
* @name sysPost
* @descriptions 岗位数据层
* @author 寒锋
* @date 2023-12-15 11:26:39
 */

package dao

import (
	"admin-go-api/api/entity"
	"admin-go-api/common/util"
	. "admin-go-api/pkg/db"
	"time"
)

// GetSysPostByCode 根据编码获取岗位信息
func GetSysPostByCode(postCode string) (sysPost entity.SysPost) {
	// sql := Db.Where("postCode = ?", postCode)
	// if id != 0 {
	// 	sql = sql.Where("post_id !=", id)
	// }
	// sql.First(&sysPost)
	Db.Where("post_code = ?", postCode).First(&sysPost)
	return sysPost
}

// GetSysPostByName 根据岗位名称获取岗位信息
func GetSysPostByName(postName string) (sysPost entity.SysPost) {
	Db.Where("post_name = ?", postName).First(&sysPost)
	return sysPost
}

// CreateSysPost 新增岗位
func CreateSysPost(sysPost entity.CreateSysPostDto) bool {
	// 验证岗位编码是否已存在
	sysPostCode := GetSysPostByCode(sysPost.PostCode)
	if sysPostCode.PostId > 0 {
		return false
	}
	// 验证岗位名称是否已存在
	sysPostName := GetSysPostByName(sysPost.PostName)
	if sysPostName.PostId > 0 {
		return false
	}
	// 生成插入数据
	data := entity.SysPost{
		PostCode:   sysPost.PostCode,
		PostName:   sysPost.PostName,
		PostStatus: sysPost.PostStatus,
		CreateTime: util.HTime{Time: time.Now()},
		Remark:     sysPost.Remark,
	}
	// 插入数据库
	result := Db.Save(&data)
	if result.RowsAffected > 0 {
		return true
	}
	return false
}

// GetSysPostById 根据id查询岗位
func GetSysPostById(postId int) (sysPost entity.SysPost) {
	Db.First(&sysPost, postId)
	return sysPost
}

// UpdateSysPost 更新岗位
func UpdateSysPost(params entity.SysPost) (sysPost entity.SysPost) {
	Db.First(&sysPost, params.PostId)
	sysPost.PostName = params.PostName
	sysPost.PostCode = params.PostCode
	sysPost.PostStatus = params.PostStatus
	if params.Remark != "" {
		sysPost.Remark = params.Remark
	}
	Db.Save(&sysPost)
	return sysPost
}

// DeleteSysPostById 根据id删除岗位
func DeleteSysPostById(dto entity.SysPostIdDto) {
	Db.Delete(&entity.SysPost{}, dto.PostId)
}

// BatchDeleteSysPostById 根据id批量删除岗位
func BatchDeleteSysPostById(dto entity.DelSysPostDto) {
	Db.Where("post_id in (?)", dto.PostId).Delete(&entity.SysPost{})
}

// UpdateSysPostStatus 修改岗位状态
func UpdateSysPostStatus(dto entity.UpdateSysPostStatusDto) {
	var sysPost entity.SysPost
	Db.First(&sysPost, dto.PostId)
	sysPost.PostStatus = dto.PostStatus
	Db.Save(&sysPost)
}

// GetSysPostList 分页查询岗位列表
func GetSysPostList(PageNum, PageSize int, PostName, PostCode string, PostStatus int, BeginTime, EndTime string) (sysPost []entity.SysPost, count int64) {
	model := Db.Table("sys_post")
	if PostName != "" {
		model = model.Where("post_name = ?", PostName)
	}
	if PostCode != "" {
		model = model.Where("post_code = ?", PostCode)
	}
	if PostStatus != 0 {
		model = model.Where("post_status = ?", PostStatus)
	}
	if BeginTime != "" && EndTime != "" {
		model = model.Where("`create_time` BETWEEN ? AND ?", BeginTime, EndTime)
	}
	model.Count(&count)
	model.Limit(PageSize).Offset((PageNum - 1) * PageSize).Order("create_time desc").Find(&sysPost)
	return sysPost, count
}

// QuerySelectSysPost 岗位下拉列表
func QuerySelectSysPost() (selectSysPost []entity.SelectSysPost) {
	Db.Table("sys_post").Select("post_id,post_name").Scan(&selectSysPost)
	return selectSysPost
}
